package com.atguigu.chapter;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

/**
 * @ClassName: StreamWordCount
 * @Description:
 * @Author: kele
 * @Date: 2021/3/31 10:34
 **/
public class StreamWordCount {

    public static void main(String[] args) throws Exception {

        //1、获取stream
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        //设置单线程
        env.setParallelism(1);

        DataStreamSource<String> ds = env.readTextFile("in/wc.txt");

        SingleOutputStreamOperator<Tuple2<String, Long>> ds1 = ds.flatMap(new FlatMapFunction<String, Tuple2<String, Long>>() {
            @Override
            public void flatMap(String line, Collector<Tuple2<String, Long>> collector) throws Exception {

                String[] words = line.split(" ");

                for (String word : words) {
                    collector.collect(Tuple2.of(word, 1l));
                }

            }
        });

        // keyBy数据类型不会做任何的变化
        KeyedStream<Tuple2<String, Long>, String> ds2 = ds1.keyBy(new KeySelector<Tuple2<String, Long>, String>() {
            @Override
            public String getKey(Tuple2<String, Long> words) throws Exception {
                return words.f0;
            }
        });

        SingleOutputStreamOperator<Tuple2<String, Long>> sum = ds2.sum(1);

        sum.print();

        env.execute();


    }

}
